var SubGameLayer = cc.Layer.extendEX({
    ctor: function () {
        this._super();
    },

    onEnter: function () {
        cc.Layer.prototype.onEnter.call(this);
        // cc.log('游戏场景启动！');

    },

    /**
     * 初始化游戏场景
     * @param data
     * @param roomDataType
     */
    initGameLayer: function (data, roomDataType) {
        // var isReplay = (roomDataType == ROOM_DATA_TYPE.REPLAY);
        // var isReconnect = (roomDataType == ROOM_DATA_TYPE.RECONNECT);
        var reconnectData = this._getRoomData(data, roomDataType);
        var key = findKey(app.map_id, gameData.mapId);
        if (!app.map_wanfa[key]) {
            cc.error('map_id:' + gameData.mapId + ' 没有对应的玩法场景');
            return;
        }
        var _gameLayer = null;
        if (app.map_wanfa[key] == 'huzi') {
            _gameLayer = this._initGame(reconnectData, roomDataType);
        }
        this._gameLayer = _gameLayer;
        return _gameLayer;
    },
    /**
     * 初始化游戏玩法
     * @private
     */
    _initGame: function (reconnectData, roomDataType) {
        var isReplay = (roomDataType == ROOM_DATA_TYPE.REPLAY);
        // var reconnectData = this._getRoomData(data, roomDataType);
        var gameLayer = new PhzLayer((isReplay ? null : reconnectData), isReplay);
        this.addChild(gameLayer);

        window.maLayer = gameLayer;
        window.maPlayBackLayer = gameLayer;
        // 胡子在游戏中添加了 回放接口。
        // if (isReplay) {
        //     window.maPlayBackLayer = new PlayerBackLayer(reconnectData);
        //     gameLayer.addChild(window.maPlayBackLayer);
        // }
        return gameLayer;
    },

    /**
     * 根据data和roomDataType转换出房间数据
     * @param data
     * @param roomDataType
     * @returns {*}
     * @private
     */
    _getRoomData: function (data, roomDataType) {
        gameData.wanfatype = 'go';
        mRoom.wanfatype = '';
        var curData = data;
        var tData = data;

        // 初始化房间数据时重置
        mRoom.sipai = false;
        mRoom['CardList'] = null;
        gameData.roomId = curData['room_id'] || curData['RoomID'];

        if (roomDataType == ROOM_DATA_TYPE.REPLAY) {//回放
            gameData.isReplay = true;
            if (data['3002']) {
                curData = data['3002']['data'];
                gameData.last3002 = data['3002'];
                tData = data;
            } else {
                if (gameData.mapId >= 300) {
                    mRoom.isReplay = true;
                    mRoom.replayData = new ReplayData(curData);
                    if (gameData.recordIds && gameData.recordIds[1])
                        mRoom.replayData.round = gameData.recordIds[1];
                    else
                        mRoom.replayData.round = 1;

                    var newdata = curData;
                    var option = newdata[0].data.Option;
                    var obj = JSON.parse(option);
                    gameData.roomId = newdata[0].data.RoomID;
                    this.getLeiyangSipai(newdata);
                    mRoom.replayTime = newdata[1].data.StartTime;
                    mRoom.getWanfa(obj);
                }
            }
        }

        gameData.mapId = curData['map_id'];
        if (!gameData.mapId) {
            var opt = curData['Option'] || curData[0]['data']['Option'] || '';
            if (opt) {
                opt = JSON.parse(opt);
                gameData.mapId = opt['mapid'];
            }
        }

        gameData.mapName = curData['map_name'];
        gameData.players = null;
        if (curData['players'])
            gameData.players = deepCopy(curData['players']);
        if (!gameData.players) {
            gameData.players = deepCopy(curData['Players'] || curData['Users']);
        }
        gameData.ownerUid = curData['owner_uid'] || curData['owner'];
        gameData.zhuangUid = curData['zhuang_uid'];
        gameData.wanfaDesp = curData['desp'];
        gameData.leftRound = curData['left_round'];
        if (gameData.leftRound == undefined) {
            gameData.leftRound = curData['total_round'] - 1;
        }
        gameData.diceData = curData['diceNum'];
        gameData.totalRound = curData['total_round'];
        gameData.anticheating = curData['anticheating'] || false;
        gameData.roomStatus = curData['room_status'] || 1;
        gameData.playerNum = curData['max_player_cnt'];
        if (!gameData.playerNum) {
            if (curData['Option'])
                gameData.playerNum = mRoom.getPlayerNum2(curData['Option']);
        }
        if (!gameData.playerNum) {
            cc.log('---curData:', curData);
        } else if (gameData.playerNum == 1) {
            cc.log('---gameData.playerNum', gameData.playerNum);
        }
        gameData.daikaiPlayer = curData['daikai_player'];
        gameData.shipin = curData['shipin'] || 0;
        gameData.last3002 = curData;
        gameData.curRound = curData['cur_round'];
        gameData.roomClubId = curData['club_id'];
        gameData.AutoReady = true;
        gameData.distance = curData['distance'] || 100;
        gameData.options = curData['options'] || curData['Option'];
        if (!gameData.options) {
            if (curData[0] && curData[0]['data'])
                gameData.options = curData[0]['data']['Option'];
        }
        if (typeof (gameData.options) == 'string') {
            gameData.options = JSON.parse(gameData.options);
        }
        if (gameData.options) {
            if (gameData.options['desp']) {
                mRoom.wanfa = gameData.options['desp'];
            }
            if (gameData.options['sipai']) {
                mRoom.sipai = gameData.options['sipai'];
            }
            if (gameData.options['club_id']) {
                gameData.roomClubId = gameData.options['club_id'];
            }
            if (gameData.options['robot']) {
                gameData.robot = gameData.options['robot'];
            }
            if (gameData.options['checkDistance']) {
                gameData.anticheating = gameData.options['checkDistance'];
            }
            if (gameData.options['AutoReady'] != undefined && gameData.options['AutoReady'] != null) {
                gameData.AutoReady = curData['AutoReady'];
            }
            if (gameData.options['distance']) {
                gameData.distance = gameData.options['distance'];
            }
            if (gameData.options['yunxujiaru']) {
                gameData.yunxujiaru = gameData.options['yunxujiaru'];
            }
        }

        return tData;
    },
    getLeiyangSipai: function (data) {
        for (var i = 0; i < data.length; i++) {
            var item = data[i];
            if (item['cmd'] == 105) {
                if (item['data'] && item['data'].Sipai) {
                    gameData.Sipai = item['data'].Sipai;
                    return;
                }
            }
        }
    },

});


// old spine。
var createSp_hz = function (res_name) {
    if (!huzi.res['sp_' + res_name + '_json'] || !huzi.res['sp_' + res_name + '_atlas']) {
        // cc.log('spine files not exist, res_name = ' + res_name);
        return null;
    }
    // cc.log('动画名字:' + ('sp_' + res_name + '_json'));
    return new sp.SkeletonAnimation(huzi.res['sp_' + res_name + '_json'], huzi.res['sp_' + res_name + '_atlas']);
};

var playSpAnimation_hz = function (sp_anim, animation_name, isLoop, timeScale, cb) {
    if (typeof sp_anim === 'string') {
        sp_anim = createSp_hz(sp_anim);
    }
    if (sp_anim instanceof sp.SkeletonAnimation) {
        sp_anim.setAnimation(track_idx++,
            _.isUndefined(animation_name) ? 'animation' : animation_name,
            _.isUndefined(isLoop) ? true : isLoop);
        if (timeScale) {
            sp_anim.setTimeScale(timeScale);
        }
        if (cb) {
            sp_anim.setCompleteListener(function () {
                cb();
            });
        }
        return sp_anim;
    }
    return null;
};